package com.zbkj.common.utils;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.zbkj.common.dto.TuHuLocationDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.util.*;


/**
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 * httpClient 工具类
 */
@Slf4j
@Component
public class RestTemplateUtil {

    private static final Logger logger = LoggerFactory.getLogger(RestTemplateUtil.class);
    @Autowired
    private RestTemplate restTemplate;


    public static final String WXPAYSDK_VERSION = "WXPaySDK/3.0.9";
    public static final String USER_AGENT = WXPAYSDK_VERSION +
            " (" + System.getProperty("os.arch") + " " + System.getProperty("os.name") + " " + System.getProperty("os.version") +
            ") Java/" + System.getProperty("java.version") + " HttpClient/" + HttpClient.class.getPackage().getImplementationVersion();

    /**
     * 设置超时时间
     */
    public RestTemplateUtil() {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        //30s
        requestFactory.setConnectTimeout(600 * 1000);
        requestFactory.setReadTimeout(600 * 1000);
        restTemplate = new RestTemplate(requestFactory);
    }

    /**
     * 发送GET请求
     *
     * @param url
     * @param param
     * @return
     */

    public String getData(String url, Map<String, String> param) {
        // 请勿轻易改变此提交方式，大部分的情况下，提交方式都是表单提交
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        return restTemplate.getForEntity(url, String.class, param).getBody();
    }

    /**
     * 发送GET请求
     *
     * @param url
     * @return
     */

    public String getLink(String url) {
        return restTemplate.getForEntity(url, String.class).getBody();
    }


    /**
     * 发送GET请求
     *
     * @param url
     * @return
     */

    public JSONObject getData(String url) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<Map<String, Object>> requestEntity =
                new HttpEntity<>(headers);

        return restTemplate.exchange(url, HttpMethod.GET, requestEntity, JSONObject.class).getBody();
    }

    /**
     * 发送GET请求 支持header
     *
     * @param url
     * @return
     */

    public JSONObject getData(String url, HashMap<String, String> hashMap) {

        HttpHeaders headers = new HttpHeaders();
        headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        for (Map.Entry<String, String> entry : hashMap.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }

        HttpEntity<Map<String, Object>> requestEntity =
                new HttpEntity<>(headers);

        return restTemplate.exchange(url, HttpMethod.GET, requestEntity, JSONObject.class).getBody();
    }

    /**
     * 发送GET请求 支持header
     *
     * @param url
     * @return
     */

    public JSONObject getDataForm(String url, MultiValueMap<String, Object> params, Map<String, String> header) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        if (CollUtil.isNotEmpty(header)) {
            for (Map.Entry<String, String> entry : header.entrySet()) {
                headers.add(entry.getKey(), entry.getValue());
            }
        }
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        return restTemplate.exchange(url, HttpMethod.GET, requestEntity, JSONObject.class).getBody();
    }


    /**
     * 发送POST-JSON请求
     *
     * @param url
     * @param param
     * @return
     */

    public String postJsonData(String url, JSONObject param) {

        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(param, headers);

        return restTemplate.postForEntity(url, param, String.class).getBody();

    }


    /**
     * 发送POST-JSON请求
     *
     * @param url
     * @param param
     * @return
     */

    public byte[] postJsonDataToObject(String url, JSONObject param) {

        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_JSON);

        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(param, headers);

        return restTemplate.postForObject(url, requestEntity, byte[].class);

    }


    /**
     * 发送POST-JSON请求
     *
     * @param url
     * @param param
     * @return
     */

    public Map postJsonDataToObj(String url, JSONObject param, String aiToken) {

        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_JSON);

        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Authorization", "Bearer " + aiToken);

        HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(param, headers);
        Map map = new HashMap();
        try {
            ResponseEntity<Object> objectResponseEntity = restTemplate.postForEntity(url, requestEntity, Object.class);
            LinkedHashMap body = (LinkedHashMap) objectResponseEntity.getBody();
            LinkedHashMap data = (LinkedHashMap) body.get("data");
            String resultUrl = (String) body.get("result_url");
            map.put("code", 200);
            map.put("resultUrl", resultUrl);
            map.put("data", data);
            return map;
        } catch (HttpClientErrorException | HttpServerErrorException e) {
            Integer statusCode = e.getStatusCode().value();
            String responseBody = e.getResponseBodyAsString();
            System.out.println("错误状态码: " + statusCode);
            System.out.println("响应体: " + responseBody);
            map.put("code", statusCode);
            switch (statusCode) {
                //当前排队人数（{request_size}人），预计等待时间约{wait_min}分钟，是否继续?
                case 202:
                    map.put("message", "当前排队人数");
                    break;
                //203已再生成队列中，请稍后！
                case 203:
                    map.put("message", "已再生成队列中，请稍后！");
                    break;
                //Token失效，需要重新认证
                case 401:
                    map.put("message", "Token失效，需要重新认证");
                    break;
                //请求超时，请稍后重试
                case 408:
                    map.put("message", "请求超时，请稍后重试");
                    break;
                //模型正在被其他请求使用，请稍后再试！
                case 428:
                    map.put("message", "模型正在被其他请求使用，请稍后再试！");
                    break;
                //当前请求人数较多，请稍后再试！
                case 429:
                    map.put("message", "当前请求人数较多，请稍后再试！");
                    break;
                //图片生成失败，请稍后再试！（模型服务生成异常）
                case 500:
                    map.put("message", "图片生成失败，请稍后再试！（模型服务生成异常）");
                    break;
            }
            return map;
        }

    }

    public Map postToRequestHash(String url, JSONObject param, String aiToken) {

        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_JSON);

        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Authorization", "Bearer " + aiToken);

        HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(param, headers);
        Map map = new HashMap();
        try {
            ResponseEntity<Object> objectResponseEntity = restTemplate.postForEntity(url, requestEntity, Object.class);
            LinkedHashMap body = (LinkedHashMap) objectResponseEntity.getBody();
            String taskId = (String) body.get("task_id");
            map.put("code", 200);
            map.put("taskId", taskId);
            return map;
        } catch (HttpClientErrorException | HttpServerErrorException e) {
            Integer statusCode = e.getStatusCode().value();
            String responseBody = e.getResponseBodyAsString();
            System.out.println("错误状态码: " + statusCode);
            System.out.println("响应体: " + responseBody);
            map.put("code", statusCode);
            switch (statusCode) {
                //当前排队人数（{request_size}人），预计等待时间约{wait_min}分钟，是否继续?
                case 201:
                    map.put("message", "当前排队人数");
                    break;
                //203已再生成队列中，请稍后！
                case 203:
                    map.put("message", "已再生成队列中，请稍后！");
                    break;
                //Token失效，需要重新认证
                case 401:
                    map.put("message", "Token失效，需要重新认证");
                    break;
                //请求超时，请稍后重试
                case 408:
                    map.put("message", "请求超时，请稍后重试");
                    break;
                //模型正在被其他请求使用，请稍后再试！
                case 428:
                    map.put("message", "模型正在被其他请求使用，请稍后再试！");
                    break;
                //当前请求人数较多，请稍后再试！
                case 429:
                    map.put("message", "当前请求人数较多，请稍后再试！");
                    break;
                //图片生成失败，请稍后再试！（模型服务生成异常）
                case 500:
                    map.put("message", "图片生成失败，请稍后再试！（模型服务生成异常）");
                    break;
            }
            return map;
        }
    }


    /**
     * 发送POST-JSON请求
     *
     * @param url
     * @param param
     * @return
     */

    public ResponseEntity postJsonDataToObjNoToken(String url, MultiValueMap<String, String> param) {

        HttpHeaders headers = new HttpHeaders();

//        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(param, headers);

        ResponseEntity<LinkedHashMap> objectResponseEntity = restTemplate.postForEntity(url, requestEntity, LinkedHashMap.class);
        return objectResponseEntity;
    }

    /**
     * 发送POST 表单请求
     *
     * @param url
     * @return
     */

    public String postFormData(String url, MultiValueMap<String, String> map) {
        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, String>> requests = new HttpEntity<MultiValueMap<String, String>>(map, headers);
        String body = restTemplate.postForEntity(url, requests, String.class).getBody();
        return body;
    }

    public String postFormData(String url, Map<String, Object> map) {
        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<Map<String, Object>> requests = new HttpEntity<>(map, headers);
        return restTemplate.postForEntity(url, requests, String.class).getBody();
    }


    /**
     * 发送POST-JSON请求
     *
     * @param url
     * @param param
     * @return
     */

    public JSONObject postJsonDataAndReturnJson(String url, JSONObject param) {
        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");

        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(param, headers);

        JSONObject body = restTemplate.postForEntity(url, requestEntity, JSONObject.class).getBody();

        return body;

    }

    /**
     * 发送POST-JSON请求
     *
     * @param url
     * @return
     */

    public JSONObject post(String url) {
        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");

        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        HttpEntity<String> requestEntity = new HttpEntity<>(headers);

        JSONObject body = restTemplate.postForEntity(url, requestEntity, JSONObject.class).getBody();

        return body;

    }

    /**
     * 发送POST-JSON请求
     *
     * @param url
     * @return
     */

    public String postXml(String url, String xml) {
        HttpHeaders headers = new HttpHeaders();

        MediaType type = MediaType.parseMediaType("application/xml; charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<String> requestEntity = new HttpEntity<>(xml, headers);

        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestEntity, String.class);
        try {
            System.out.println("responseEntity" + responseEntity);
            return new String(Objects.requireNonNull(responseEntity.getBody()).getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 发送POST-JSON请求(微信退款专用)
     *
     * @param url
     * @return
     */

    public String postWXRefundXml(String url, String xml, String mchId, String path) throws Exception {
        KeyStore clientStore = KeyStore.getInstance("PKCS12");
        // 读取本机存放的PKCS12证书文件
        FileInputStream instream = new FileInputStream(path);
        try {
            // 指定PKCS12的密码(商户ID)
            clientStore.load(instream, mchId.toCharArray());
        } finally {
            instream.close();
        }

        // 实例化密钥库 & 初始化密钥工厂
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(clientStore, mchId.toCharArray());

        // 创建 SSLContext
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(kmf.getKeyManagers(), null, new SecureRandom());

        SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
                sslContext,
                new String[]{"TLSv1"},
                null,
                new DefaultHostnameVerifier());

        BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager(
                RegistryBuilder.<ConnectionSocketFactory>create()
                        .register("http", PlainConnectionSocketFactory.getSocketFactory())
                        .register("https", sslConnectionSocketFactory)
                        .build(),
                null,
                null,
                null
        );

        HttpClient httpClient = HttpClientBuilder.create()
                .setConnectionManager(connManager)
                .build();

        HttpPost httpPost = new HttpPost(url);

        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(8 * 1000).setConnectTimeout(6 * 1000).build();
        httpPost.setConfig(requestConfig);

        StringEntity postEntity = new StringEntity(xml, "UTF-8");
        httpPost.addHeader("Content-Type", "text/xml");
        httpPost.addHeader("User-Agent", USER_AGENT + " " + mchId);
        httpPost.setEntity(postEntity);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        org.apache.http.HttpEntity httpEntity = httpResponse.getEntity();
        return EntityUtils.toString(httpEntity, "UTF-8");
    }

    /**
     * 发送POST 表单请求
     *
     * @param url
     * @param params
     * @return 表单提交，返回数据分为两部分，一个代表成功，一个是返回的数据内容
     */

    public String postFormData(String url, LinkedMultiValueMap<String, Object> params) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity =
                new HttpEntity<>(params, headers);

        ResponseEntity<String> responseEntity = restTemplate.exchange(
                url,
                HttpMethod.POST,
                requestEntity,
                String.class);

        String body = responseEntity.getBody();

        return body;

    }


    public String postMapData(String url, Map<String, Object> params) {
        //cs
        HttpHeaders headers = new HttpHeaders();
        headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        HttpEntity<Map<String, Object>> requestEntity =
                new HttpEntity<>(params, headers);

        String body1 = restTemplate.postForEntity(url, requestEntity, String.class).getBody();
        return body1;

    }

    public String postMapData(String url, Map<String, Object> params, Map<String, String> header) {

        //cs
        HttpHeaders headers = new HttpHeaders();
        headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        for (Map.Entry<String, String> entry : header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }

        HttpEntity<Map<String, Object>> requestEntity =
                new HttpEntity<>(params, headers);

        String body1 = restTemplate.postForEntity(url, requestEntity, String.class).getBody();


        System.out.println(body1);

        return body1;

    }


    public String postStringData(String url, String data) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        HttpEntity<String> requestEntity =
                new HttpEntity<>(data, headers);

        String body1 = restTemplate.postForEntity(url, requestEntity, String.class).getBody();
        return body1;

    }

    public byte[] postJsonDataAndReturnBuffer(String url, JSONObject param) {
        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(param, headers);

        return restTemplate.postForEntity(url, param, byte[].class).getBody();
    }


    public byte[] getBuffer(String url) {
        return restTemplate.getForEntity(url, byte[].class).getBody();
    }

    /**
     * post——from-urlencoded格式请求
     */
    public String postFromUrlencoded(String url, MultiValueMap<String, Object> params, Map<String, String> header) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        if (CollUtil.isNotEmpty(header)) {
            for (Map.Entry<String, String> entry : header.entrySet()) {
                headers.add(entry.getKey(), entry.getValue());
            }
        }

        HttpEntity<MultiValueMap<String, Object>> requestEntity =
                new HttpEntity<>(params, headers);

//        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
//        //30s
//        requestFactory.setConnectTimeout(30*1000);
//        requestFactory.setReadTimeout(30*1000);
//        restTemplate = new RestTemplate(requestFactory);
        return restTemplate.postForEntity(url, requestEntity, String.class).getBody();
    }


    public Map getDataToVideo(String url, String aiToken) {

        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_JSON);

        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Authorization", "Bearer " + aiToken);
        HttpEntity<String> entity = new HttpEntity<>(headers);
        Map map = new HashMap();
        try {
            ResponseEntity<Object> objectResponseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, Object.class);
            LinkedHashMap body = (LinkedHashMap) objectResponseEntity.getBody();
            String resultUrl = (String) body.get("result_url");
            String status = (String) body.get("status");
            map.put("code", 200);
            map.put("resultUrl", resultUrl);
            map.put("status", status);
            return map;
        } catch (HttpClientErrorException | HttpServerErrorException e) {
            Integer statusCode = e.getStatusCode().value();
            String responseBody = e.getResponseBodyAsString();
            System.out.println("错误状态码: " + statusCode);
            System.out.println("响应体: " + responseBody);
            map.put("code", statusCode);
            switch (statusCode) {
                case 201:
                    map.put("message", "请求正在处理中");
                    break;
                //request_hash不存在
                case 404:
                    map.put("message", "请求不存在");
                    break;
                //视频生成失败
                case 500:
                    map.put("message", "处理失败,请稍后再试!");
                    break;
            }
            return map;
        }
    }


    public Map postToFaceswap(String url, JSONObject param, String aiToken) {
        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_JSON);

        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Authorization", "Bearer " + aiToken);

        HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(param, headers);
        Map map = new HashMap();
        try {
            ResponseEntity<Object> objectResponseEntity = restTemplate.postForEntity(url, requestEntity, Object.class);
            LinkedHashMap body = (LinkedHashMap) objectResponseEntity.getBody();
            String resultUrl = (String) body.get("result_url");
            map.put("code", 200);
            map.put("resultUrl", resultUrl);
            return map;
        } catch (HttpClientErrorException | HttpServerErrorException e) {
            Integer statusCode = e.getStatusCode().value();
            String responseBody = e.getResponseBodyAsString();
            System.out.println("错误状态码: " + statusCode);
            System.out.println("响应体: " + responseBody);
            map.put("code", statusCode);
            switch (statusCode) {
                //当前排队人数（{request_size}人），预计等待时间约{wait_min}分钟，是否继续?
                case 202:
                    map.put("message", "当前排队人数");
                    break;
                //203已再生成队列中，请稍后！
                case 203:
                    map.put("message", "已再生成队列中，请稍后！");
                    break;
                //Token失效，需要重新认证
                case 401:
                    map.put("message", "Token失效，需要重新认证");
                    break;
                //请求超时，请稍后重试
                case 408:
                    map.put("message", "请求超时，请稍后重试");
                    break;
                //模型正在被其他请求使用，请稍后再试！
                case 428:
                    map.put("message", "模型正在被其他请求使用，请稍后再试！");
                    break;
                //当前请求人数较多，请稍后再试！
                case 429:
                    map.put("message", "当前请求人数较多，请稍后再试！");
                    break;
                //图片生成失败，请稍后再试！（模型服务生成异常）
                case 500:
                    map.put("message", "图片生成失败，请稍后再试！（模型服务生成异常）");
                    break;
            }
            return map;
        }

    }


    public Map getDataImageToVideo(String url, String aiToken) {
        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_JSON);

        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Authorization", "Bearer " + aiToken);
        HttpEntity<String> entity = new HttpEntity<>(headers);
        Map map = new HashMap();
        try {
            ResponseEntity<Object> objectResponseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, Object.class);
            LinkedHashMap body = (LinkedHashMap) objectResponseEntity.getBody();
            String resultUrl = (String) body.get("result_url");
            String coverUrl = (String) body.get("cover_url");
            String status = (String) body.get("status");
            map.put("code", 200);
            map.put("resultUrl", resultUrl);
            map.put("coverUrl", coverUrl);
            map.put("status", status);
            return map;
        } catch (HttpClientErrorException | HttpServerErrorException e) {
            Integer statusCode = e.getStatusCode().value();
            String responseBody = e.getResponseBodyAsString();
            System.out.println("错误状态码: " + statusCode);
            System.out.println("响应体: " + responseBody);
            map.put("code", statusCode);
            switch (statusCode) {
                case 201:
                    map.put("message", "请求正在处理中");
                    break;
                //request_hash不存在
                case 404:
                    map.put("message", "请求不存在");
                    break;
                //视频生成失败
                case 500:
                    map.put("message", "处理失败,请稍后再试!");
                    break;
            }
            return map;
        }
    }


    /**
     * 发送POST-JSON请求
     *
     * @param url
     * @param param
     * @return
     */
    public Map postTuHu(String url, JSONObject param) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED_VALUE));
//        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(param);
        Map map = new HashMap();
        try {
            ResponseEntity<Object> objectResponseEntity = restTemplate.postForEntity(url, requestEntity, Object.class);
            LinkedHashMap body = (LinkedHashMap) objectResponseEntity.getBody();
            String success = (String) body.get("success");
            TuHuLocationDto result = (TuHuLocationDto) body.get("result");
            map.put("success", success);
            map.put("result", result);
            return map;
        } catch (HttpClientErrorException | HttpServerErrorException e) {
            log.info("途狐管车获取accessToken错误...");
        }
        return map;
    }




}
